﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework2
{
    public partial class Form2 : Form
    {
        private string Num;
        private string Pin;
        private ATM atm;
        public event Action exit;
        //int op;
        enum OP
        {
            Deposit,
            Withdraw,
            ChangePin
        }
        private OP op;
        //public  
        public Form2(ATM atm)
        {
            InitializeComponent();
            /* Num = num;
            Pin = pin;*/
            this.atm = atm;
            atm.throwBigMoneyWarning += BigMoneyWarning;
            Process_Show(" ",1);
        }
        private void Operation_Show(string text)
        {
            textBox1.Text = "";
            label5.Text = text;
            label3.Hide();
            label5.Show();
            button_YES.Show();
            textBox1.Show();
        }
        private void BigMoneyWarning()
        {
            MessageBox.Show("Warning : The current withdrawal amount is larger than $10000!");
        }
        private void Process_Show(string text,int fontSize)
        {
            label5.Hide();
            textBox1.Hide();
            button_YES.Hide();
            label3.Text = text;
            //label3.Font.Size = 1;
            label3.Font = new System.Drawing.Font("Arial", fontSize);
            label3.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            op = OP.Withdraw;
            Operation_Show("Take the amount:");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process_Show("Balance:" + atm.CheckBalance(), 31);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Close();
            exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            op = OP.Deposit;
            Operation_Show("Deposit amount:");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int amount;
            if (!int.TryParse(textBox1.Text, out amount)){
                return;
            }
            switch (op)
            {
                case OP.Deposit:
                    try
                    {
                        atm.Deposit(amount);
                        Process_Show("Withdrawal successful!", 20);
                    }
                    catch (BadCashException bce)
                    {
                        
                        int colonIndex = bce.Message.IndexOf(':');

                        if (colonIndex != -1)
                        {
                            string afterColon = bce.Message.Substring(colonIndex);
                            string numberPart = afterColon.Substring(1);
                            int number = int.Parse(numberPart);
                            StringBuilder sb = new StringBuilder("\n\nThe remaining $");
                            sb.Append(amount - number).Append("\nhas been deposited.");
                            Process_Show(bce.Message + sb.ToString(), 20);
                        }
                        else
                        {
                            Process_Show(bce.Message + "/n", 20);
                        }
                    }
                    //string fakeMoney = (atm.Deposit(amount)*100).ToString();
                    //StringBuilder sb = new StringBuilder("Withdrawal successful!\nBad banknote rate:");
                    //sb.Append(fakeMoney).Append("/").Append(amount);
                    //Process_Show(sb.ToString(), 18);
                    break;
                case OP.Withdraw:
                    if (atm.Withdraw(amount))
                    {
                        Process_Show("Withdrawal successful!", 18);
                    }
                    else
                    {
                        Process_Show("Insufficient balance!", 18);
                    }
                    break;
                case OP.ChangePin:
                    if (atm.changePin(amount.ToString())){
                        Process_Show("The password change was successful", 15);
                    }
                    break;
            }
            
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            op = OP.ChangePin;
            Operation_Show("new   password:");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string cl = atm.GetCreditNum().ToString();
            Process_Show("Credit Limit:" + cl,30);
        }
    }
}
